package service.Imp;

import ExceptionLogin.LoginFailedException;
import Message.DTO.UserLoginDTO;
import Message.Prompt.MessageConstant;
import Message.entity.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import properties.WeChatProperties;
import service.UserService;
import utils.HttpClientUtil;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private WeChatProperties weChatProperties;
    @Autowired
    private UserMapper userMapper;


    @Override
    public User login(UserLoginDTO dto) throws LoginFailedException {
        // 1.通过HttpClient，构造登录凭证校验请求
        // 构造请求参数

        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("appid", weChatProperties.getAppid());
        paramMap.put("secret", weChatProperties.getSecret());
        paramMap.put("js_code", dto.getCode());
        paramMap.put("grant_type", "authorization_code");

        // 调用HttpClientUtil工具类，发送请求
      String res =  HttpClientUtil.doGet("https://api.weixin.qq.com/sns/jscode2session",paramMap);
         log.info("res={}",res);
        // 2.解析响应结果，获取openid
        JSONObject jsonObject = JSON.parseObject(res);
        String openid = jsonObject.getString("openid");
        if (openid == null) {
            throw new LoginFailedException(MessageConstant.USER_NOT_LOGIN);
        }
        // 3.判断是否为新用户，根据openid查询user表
     User user = userMapper.SelectByOpenid(openid);

        // 4.如果是新用户，初始化用户数据到user表中
        if(user == null) {
            user = new User();
            user.setOpenid(openid);
            user.setCreateTime(LocalDateTime.now());
            user.setName(openid.substring(0,5));
            userMapper.insert(user);
        }

        // 5.否则，直接返回user对象数据
        return user;
    }
}
